package org.emergency.framework.web.context;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.emergency.framework.common.Context;
import org.emergency.framework.constant.SysConfConstant;
import org.emergency.framework.otherDatasourse.ITbSysConfigDAO;
import org.emergency.framework.otherDatasourse.TbSysConfig;
import org.emergency.framework.service.common.IRolePermissionMapping;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * ServletContext监听类，用来在Servlet容器启动时执行系统角色的权限填充
 * 
 * @author zlw
 * 
 */

public class ServletContextLoaderListener implements ServletContextListener {

	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		// servletContextEvent.getServletContext().removeAttribute(
		// UserUtil.ROLE_AUTHORITIES);
	}

	// dgm..监听Servlet容器,当应用开始的时候它会调用contextInitialized()方法;
	// @Override
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		System.out.println("+++++++++tomcat启动时调用，加载静态map·········");
		ServletContext servletContext = servletContextEvent.getServletContext();// dgm..得到ServletContext

		IRolePermissionMapping rolePermissionMapping = this
				.getRolePermissionMapping(servletContext);
		rolePermissionMapping.loadRolePermission();
		// rolePermissionMapping.updateMap();
		// int t = rolePermissionMapping.queryTbPermissionByRole("1001").size();
		// System.out.println("++++++++++++根据角色ID查询权限：======"+ t);

		/**
		 * 以下为对tbsysconfig的取
		 */
		ITbSysConfigDAO tbSysConfigDAO = (ITbSysConfigDAO) WebApplicationContextUtils
				.getWebApplicationContext(servletContext).getBean("tbSysConfigDAOImpl");
		List<TbSysConfig> listsys = tbSysConfigDAO.findAll();
		
		for (TbSysConfig tbSysConfig : listsys) {
			if(tbSysConfig.getTitle().equals("ANNEX_PATH")){
				System.out.println("这里set进去 ");
				SysConfConstant.setAnnex_pathsysconfig(tbSysConfig);
			}
		}
		//附件daddtbSysConf(listsys.get(1).getTitle(), listsys.get(1));
		
		
	}

	/**
	 * 加载role树
	 * 
	 * @param servletContext
	 * @return
	 */
	protected IRolePermissionMapping getRolePermissionMapping(
			ServletContext servletContext) {
		return (IRolePermissionMapping) WebApplicationContextUtils
				.getWebApplicationContext(servletContext).getBean(
						"rolePermissionMappingImpl");// dgm..那么强大的东西是什么来的???!!得到spring
														// bean?.
	}

}
